﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using CDIO.Util;
using CDIO.ICCard;
using CDIO.DB;

namespace 卡务中心
{
    public partial class CardCenter : Form
    {
        List<string> contents = new List<string>();
        private const string KeyName = "卡务中心模块序号";
        private const string RNKey = "食堂服务器名";
        private const string RPKey = "食堂服务器端口号";
        private string restaurantServer;
        private int restaurantPort;
        public CardCenter()
        {
            InitializeComponent();
            int index = (int)RegisterOprate.ReadRegData(KeyName);
            tabs.SelectedIndex = index;
            restaurantServer = "127.0.0.1";
            restaurantPort = 4910;
        }


        private void 充值_Click(object sender, EventArgs e)
        {
            try
            {
                float MoneyF = (float)Convert.ToDouble(chargeMoney.Text);
                ushort MoneyU = (ushort)(MoneyF * 10);
                Card.Charge(Convert.ToUInt16(cardNo.Text), MoneyU);
                RefreshCard();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                chargeMoney.Text = "";
            }
        }

        private void 挂失_Click(object sender, EventArgs e)
        {
            Net send = new Net(restaurantServer, restaurantPort);

            DAO dao = new DAO();
            MySql.Data.MySqlClient.MySqlDataReader rdr = 
                (MySql.Data.MySqlClient.MySqlDataReader)dao.Excute(
                "select cardNo from cards where StuNo='" + sub_stuNo.Text + "'"
                );
            if (rdr.Read()==false)
            {
                MessageBox.Show("不存在该学号");
                return;
            }
            if (send.Send(rdr["cardNo"] as string) == false)
            {
                MessageBox.Show("发送挂失信息失败");
                return;
            }
            MessageBox.Show("挂失成功");
        }

        private void 挂失重置_Click(object sender, EventArgs e)
        {
            sub_stuNo.Text = "";
            sub_cardPsw.Text = "";
        }


        private void 注册_Click(object sender, EventArgs e)
        {
            ulong stuno = Convert.ToUInt64(reg_stuNo.Text);
            ushort cardno = Convert.ToUInt16(reg_cardNo.Text);
            DAO dao = new DAO();
            MySql.Data.MySqlClient.MySqlDataReader rdr =
                 (MySql.Data.MySqlClient.MySqlDataReader)dao.Excute("select stuName from students where stuNo = " + stuno);
            if (rdr.Read() == false)
            {
                MessageBox.Show("没有该学号");
                return;
            }
            if (Card.IssueCard(stuno, cardno, rdr["stuName"] as string, (byte)(underGraduate.Focused ? 1 : 2)) == false)
            {
                MessageBox.Show("发卡失败");
                return;
            }
            ushort predepoist = Convert.ToUInt16(reg_preDeposit.Text);
            if (predepoist > 0)
            {
                Card.Charge(cardno, (ushort)(predepoist * 10));
            }

        }

        private void 注册重置_Click(object sender, EventArgs e)
        {
            reg_cardNo.Text = "";
            reg_stuNo.Text = "";
            reg_preDeposit.Text = "";
        }


        private void tabs_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.KeyCode >= Keys.F1 && e.KeyCode <= Keys.F4 && e.Alt == false)
                tabs.SelectedIndex = e.KeyCode - Keys.F1;
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            RegisterOprate.SaveRegData(KeyName, tabs.SelectedIndex);
            if(MessageBox.Show("确认退出?","请确认",MessageBoxButtons.YesNo) == DialogResult.No)
                e.Cancel = true;
            base.OnClosing(e);
        }

        private void  RefreshCard()
        {
            string ReadStuName;
            ushort ReadCardNo;
            long ReadStuNo;
            ushort ReadBlance;
            float ReadBalnceF;
            Card.ReadCard(out ReadCardNo, out ReadStuNo, out ReadStuName, out ReadBlance);
            switch (tabs.SelectedIndex)
            {
                case 0://充值页面
                    ReadBalnceF = ReadBlance / 10.0f;
                    this.StuName.Text = ReadStuName;
                    this.stuNo.Text = ReadStuNo.ToString("D9");
                    this.cardNo.Text = ReadCardNo.ToString("D5");
                    this.Blance.Text = ReadBalnceF.ToString("F1");
                    this.chargeMoney.ReadOnly = false;
                    this.chargeMoney.Focus();
                    break;
                case 1://注册页面
                    reg_cardNo.ReadOnly = false;
                    reg_stuNo.ReadOnly = false;
                    reg_preDeposit.ReadOnly = false;
                    underGraduate.Enabled = true;
                    graduate.Enabled = true;

                    break;
            }
        }

        private uint PreCardSerial;
        private void CardDetectTimer_Tick(object sender, EventArgs e)
        {
            uint CurCardSerial = Card.DetectedCard;
            if (CurCardSerial != 0)
            {
                if (CurCardSerial != PreCardSerial)
                    RefreshCard();
            }
            PreCardSerial = CurCardSerial;
        }
        private int ReadRegConfig()
        {
            int readed = 0;
            object regval = RegisterOprate.ReadRegData(RNKey);
            if (regval != null)
            {
                restaurantServer = regval as string;
                readed++;
            }
            object RegPort = RegisterOprate.ReadRegData(RPKey);
            if (regval != null)
            {
                restaurantPort = (int)RegPort;
                readed++;
            }

            return readed;
        }

        private void SaveRegConfig()
        {
            RegisterOprate.SaveRegData(RNKey, restaurantServer);
            RegisterOprate.SaveRegData(RPKey, restaurantPort);
        }


        private void Configure()
        {
            ConfigDialog c = new ConfigDialog();
            c.servername.Text = restaurantServer;
            c.port.Text = restaurantPort.ToString();
            if (DialogResult.OK == c.ShowDialog(this))
            {
                restaurantServer = c.servername.Text;
                restaurantPort = Convert.ToInt32(c.port.Text);
            }
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Configure();
        }

    }
}
